package com.example.spark.service.minio;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import com.example.spark.config.MinioCloudStorageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class MinioCloudStorageService extends AbstractCloudStorageService {
    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioCloudStorageConfig configuration;




    /**
     * 文件上传
     *
     * @param data 文件字节数组
     * @param path 文件路径，包含文件名
     * @return 返回http地址
     */
    @Override
    public String uploadPhoto(byte[] data, String path) {
        return uploadPhoto(new ByteArrayInputStream(data), path);
    }

    @Override
    public String uploadFile(byte[] data, String path,String currentType) {
        return uploadFile(new ByteArrayInputStream(data), path,currentType);
    }

    @Override
    public String uploadVideo(byte[] data, String path) {
        return uploadVideo(new ByteArrayInputStream(data), path);
    }

    /**
     * 文件上传
     *
     * @param data   文件字节数组
     * @param suffix 后缀
     * @return 返回http地址
     */
    // 上传图片方法
    @Override
    public String uploadPhotoSuffix(byte[] data, String suffix, String name) {
        System.out.println(configuration);
        return uploadPhoto(data, getPath(configuration.getMinioPrefix(), suffix, name));
    }

    //上传文件方法
    @Override
    public String uploadFileSuffix(byte[] data, String suffix, String name,String currentType) {
        System.out.println(configuration);
        return uploadFile(data, getPath(configuration.getMinioPrefix(), suffix, name),currentType);
    }

    // 上传视频方法
    @Override
    public String uploadVideoSuffix(byte[] data, String suffix, String name) {
        System.out.println(configuration);
        return uploadVideo(data, getPath(configuration.getMinioPrefix(), suffix, name));
    }


    /**
     * 文件上传
     *
     * @param inputStream 字节流
     * @param path        文件路径，包含文件名
     * @return 返回http地址
     */
    @Override
    public String uploadPhoto(InputStream inputStream, String path) {
        // 使用putObject上传一个文件到存储桶中。
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(configuration.getBucketName())
                    .object(path)
                    .stream(inputStream, inputStream.available(), -1)
                    .contentType("image/jpeg")//设置传递文件的内容类型，不需要下载就能直接预览
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return configuration.getUrl() + "/" + configuration.getBucketName() + "/" + path;
    }

    @Override
    public String uploadFile(InputStream inputStream, String path,String currentType) {
        // 使用putObject上传一个文件到存储桶中。
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(configuration.getBucketName())
                    .object(path)
                    .stream(inputStream, inputStream.available(), -1)
                    .contentType(currentType)//设置传递文件的内容类型，不需要下载就能直接预览
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return configuration.getUrl() + "/" + configuration.getBucketName() + "/" + path;
    }

    @Override
    public String uploadVideo(InputStream inputStream, String path) {
        // 使用putObject上传一个文件到存储桶中。
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(configuration.getBucketName())
                    .object(path)
                    .stream(inputStream, inputStream.available(), -1)
                    .contentType("video/mp4")//设置传递文件的内容类型，不需要下载就能直接预览
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return configuration.getUrl() + "/" + configuration.getBucketName() + "/" + path;
    }


    /**
     * 文件上传
     *
     * @param inputStream 字节流
     * @param suffix      后缀
     * @return 返回http地址
     */
    @Override
    public String uploadPhotoSuffix(InputStream inputStream, String suffix, String name) {
        return uploadPhoto(inputStream, getPath(configuration.getMinioPrefix(), suffix, name));
    }

    @Override
    public String uploadFileSuffix(InputStream inputStream, String suffix, String name , String currentType) {
        return uploadFile(inputStream, getPath(configuration.getMinioPrefix(), suffix, name),currentType);
    }


    @Override
    public String uploadVideoSuffix(InputStream inputStream, String suffix, String name) {
        return uploadVideo(inputStream, getPath(configuration.getMinioPrefix(), suffix, name));
    }
}

